package org.mozhu.mboot.core.json.jackson.jdk.ser;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;

public final class DateSerializer extends org.mozhu.mboot.core.json.jackson.jdk.ser.JdkSerializerBase<Date> {
	private static final long serialVersionUID = -1242421290023117385L;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public DateSerializer() {
		super(Date.class);
	}

	@Override
	public void serialize(Date dt, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
		if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
			// Timestamp here actually means number
			jgen.writeNumber(dt.getTime());
		} else {
			jgen.writeString(sdf.format(dt));
		}
	}

	@Override
	public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type typeHint) {
		return createSchemaNode(provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) ? "number" : "string", true);
	}

}